<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die();

jimport('joomla.application.component.model');

class ArticleflowModelHistory_edit extends JModel {

    var $_data;
    var $_id;

    function __construct($config = array()) {

        parent::__construct($config);
        $array = JRequest::getVar('cid', array(), '', 'array');
        $edit = JRequest::getVar('edit', true);

        if ($edit) {

            $this->setId((int) $array[0]);
        }
    }
      function setId($id) {
        $this->_id = $id;
        $this->_data = null;
    }

      function getData() {
        if (!$this->_Loaddata()) {
            $this->_initData();
        }
        else
            return $this->_data;
    }

    function _Loaddata() {
        if (empty($this->_data)) {
            $query = 'SELECT a.*,ss.title as section'
                    . ' FROM #__content_temp_edit AS a'
                    . ' LEFT JOIN #__sections AS ss ON ss.id = a.sectionid'
                    . ' WHERE a.id = ' . (int) $this->_id;

            $this->_db->setQuery($query);
            $this->_data = $this->_db->loadObject();
            return (boolean) $this->_data;
        }
        return true;
    }
    function _initData() {
        if (empty($this->_data)) {
            $article = new stdClass();
            $article->id = 0;

            $article->published = 1;
            $article->title = '';
            $article->created_by = 1;
            $article->modified_by = 1;
            $article->created = gmdate('Y-m-d H:i:s');
            //$article->modified = gmdate('Y-m-d H:i:s');
            $this->_data = $article;
            return (boolean) $this->_data;
        }
        return true;
    }

      function checkout($uid = null) {
        if ($this->_id) {
            // Make sure we have a user id to checkout the article with
            if (is_null($uid)) {
                $user = & JFactory::getUser();
                $uid = $user->get('id');
            }
            // Lets get to it and checkout the thing...
            $weblink = & $this->getTable('articleflow_temp_edit');
            if (!$weblink->checkout($uid, $this->_id)) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }

            return true;
        }
        return false;
    }

}

?>
